﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
public partial class admin_Settings_SetValue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
        grid.RowDeleting += new GridViewDeleteEventHandler(grid_RowDeleting);
        grid.RowUpdating += new GridViewUpdateEventHandler(grid_RowUpdating);
        grid.RowEditing += new GridViewEditEventHandler(grid_RowEditing);
        grid.RowCancelingEdit += delegate { Response.Redirect(Request.RawUrl); };
        grid.RowDataBound += new GridViewRowEventHandler(grid_RowDataBound);
        grid.PageIndexChanging += new GridViewPageEventHandler(grid_PageIndexChanging);
    }
    

    void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // throw new NotImplementedException();
        grid.PageIndex = e.NewPageIndex;
        BindGrid();
    }

    void BindGrid()
    {

        grid.DataSource = SettingManager.FindAll();
        grid.DataBind();
    }


    void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit ||
                e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
        {
           // Setting self = (Setting)e.Row.DataItem;
        }
    }
    void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
       // grid.EditIndex = e.NewEditIndex;
       // BindGrid();
    }
    void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //string txtSettingName = (string)grid.DataKeys[e.RowIndex].Value;

        //TextBox txtSettingValue = (TextBox)grid.Rows[e.RowIndex].FindControl("txtSettingValue");
        //Setting.SetValue(txtSettingName, txtSettingValue.Text);
        //Response.Redirect(Request.RawUrl);
    }

    void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        string txtSettingName = (string)grid.DataKeys[e.RowIndex].Value;
        Setting cat = SettingManager.FindByName(txtSettingName);
        SettingManager.Delete(cat.SettingId);
        Response.Redirect(Request.RawUrl);

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtSettingName.Text) && !string.IsNullOrEmpty(txtSettingValue.Text))
        {
            if (SettingManager.Insert(new Setting { Name = txtSettingName.Text, Value = txtSettingValue.Text }))
                Response.Redirect(Request.RawUrl);
        }
    }
}